﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhDataAcess.produceDAL;
using dhSystemBLL.produceBLL;
using dhSystemBLL.orderData;
using dhDataAcess.produceLinq;
using dhDataAcess.baseBLL;
using baseSet.importExport;


namespace dhsystem.produceForm
{
    public partial class FrmrlckDetailAdd : Form
    {
        rlckBLL rlck = new rlckBLL();
        rlckDetailModel rlckDetailRow = new rlckDetailModel();
        materialListBLL materialList = new materialListBLL();
        BindingSource BindingSourcerlckDetailAdd;
        string state;
        string billNo;
        //绑定数据源
        public FrmrlckDetailAdd(BindingSource bindingSourcerlckDetail, string _state, string _billNo)
        {
            InitializeComponent();
            BindingSourcerlckDetailAdd = bindingSourcerlckDetail;
            state = _state;
            billNo = _billNo;
        }
        //打开窗口时绑定数据
        private void FrmrlckDetailAdd_Load(object sender, EventArgs e)
        {
      
         

            if (state == "编辑")
            {
                textbillNo.DataBindings.Add("text", BindingSourcerlckDetailAdd, "billNo");
                textmatNum.DataBindings.Add("text", BindingSourcerlckDetailAdd, "matNum");
                textmatName.DataBindings.Add("text", BindingSourcerlckDetailAdd, "matName");
                textmatSpec.DataBindings.Add("text", BindingSourcerlckDetailAdd, "matSpec");
                textqty.DataBindings.Add("text", BindingSourcerlckDetailAdd, "qty");
                textunit.DataBindings.Add("text", BindingSourcerlckDetailAdd, "unit");
                textfqty.DataBindings.Add("text", BindingSourcerlckDetailAdd, "fqty");
                comboBoxfunit.DataBindings.Add("text", BindingSourcerlckDetailAdd, "funit");
                textmark.DataBindings.Add("text", BindingSourcerlckDetailAdd, "mark");
                //textid.DataBindings.Add("text", BindingSourcescrlDetailAdd, "recordid");
                // BindingSourceDetailAdd.AddNew();
                //textproductname.Text = productname;
              
            }
            if (state == "新增")
            {
                textbillNo.DataBindings.Add("text", BindingSourcerlckDetailAdd, "billNo");
                textmatNum.DataBindings.Add("text", BindingSourcerlckDetailAdd, "matNum");
                textmatName.DataBindings.Add("text", BindingSourcerlckDetailAdd, "matName");
                textmatSpec.DataBindings.Add("text", BindingSourcerlckDetailAdd, "matSpec");
                textqty.DataBindings.Add("text", BindingSourcerlckDetailAdd, "qty");
                textunit.DataBindings.Add("text", BindingSourcerlckDetailAdd, "unit");
                textfqty.DataBindings.Add("text", BindingSourcerlckDetailAdd, "fqty");
                comboBoxfunit.DataBindings.Add("text", BindingSourcerlckDetailAdd, "funit");
                textmark.DataBindings.Add("text", BindingSourcerlckDetailAdd, "mark");
                // textid.DataBindings.Add("text", BindingSourcescrlDetailAdd, "recordid");
                BindingSourcerlckDetailAdd.AddNew();
                textbillNo.Text = billNo;
            }

            textmatSpec.Focus();
            comboBoxfunit.Text = "件";
            //bindingSourcematerialList.DataSource = materialList.getmaterialListAll();
            //comboBoxmatNum.DataSource = bindingSourcematerialList;
            //comboBoxmatNum.DisplayMember = "matName";
            //comboBoxmatNum.ValueMember = "matNum";
        }
        //保存按钮，插入或修改数据
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (textqty.Text == "")
            {
                MessageBox.Show("请输入数量");

            }
            else
            {
                try
                {
                    DialogResult result = MessageBox.Show("是否保存资料！", "提示信息", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {

                        BindingSourcerlckDetailAdd.EndEdit(); //编辑到完成，写到数据源
                        BindingSourcerlckDetailAdd.CancelEdit(); //刷新数据作用

                    }
                }

                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
        }

        //取消按钮
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            BindingSourcerlckDetailAdd.CancelEdit();
            this.Close();
        }
        //原材料值发生改变之后
        private void comboBoxmatNum_SelectedIndexChanged(object sender, EventArgs e)
        {
           // textunit.Text = basicSql.getunitbymaterialList(comboBoxmatNum.SelectedValue.ToString());
        }
        //选择原材料
        private void buttworkerselect_Click(object sender, EventArgs e)
        {
            string field;
            field = "matNum";
            Frmprodutselect productselect = new Frmprodutselect(moduleInfo.materialList, field);
            DialogResult result = productselect.ShowDialog();
            if (result == DialogResult.Yes)
            {
                textmatNum.Text  = productselect.pname;
            }
        }

        //原材料值发生改变之后
        private void textmatNum_TextChanged(object sender, EventArgs e)
        {
            try
            {
                textunit.Text = basicSql.getunitbymaterialList(textmatNum.Text);
                textmatSpec.Text = basicSql.getmatSpecbymaterialList(textmatNum.Text);
                textmatName.Text = basicSql.getmatNamebymaterialList(textmatNum.Text);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
    }
}
